﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Mvc.Html;
using System.Diagnostics.CodeAnalysis;
using System.Web.Routing;

namespace Prototipo.Helpers
{
    public static class AnchorExtensions
    {
        public static MvcAnchor BeginAnchor(this HtmlHelper htmlHelper, ActionResult action)
        {
            return BeginAnchor(htmlHelper, action, null);
        }

        public static MvcAnchor BeginAnchor(this HtmlHelper htmlHelper, ActionResult action, object htmlAttributes)
        {
            return BeginAnchor(htmlHelper, action, HtmlHelper.AnonymousObjectToHtmlAttributes(htmlAttributes));
        }

        public static MvcAnchor BeginAnchor(this HtmlHelper htmlHelper, ActionResult action, IDictionary<string, object> htmlAttributes)
        {
            string linkAction = UrlHelper.GenerateUrl("Default",
                                                      action.GetT4MVCResult().Action,
                                                      action.GetT4MVCResult().Controller,
                                                      action.GetRouteValueDictionary(),
                                                      htmlHelper.RouteCollection,
                                                      htmlHelper.ViewContext.RequestContext,
                                                      includeImplicitMvcValues: false);
            return AnchorHelper(htmlHelper, linkAction, htmlAttributes);
        }

        public static void EndAnchor(this HtmlHelper htmlHelper)
        {
            EndAnchor(htmlHelper.ViewContext);
        }

        internal static void EndAnchor(ViewContext viewContext)
        {
            viewContext.Writer.Write("</a>");
            viewContext.OutputClientValidation();
        }

        [SuppressMessage("Microsoft.Reliability", "CA2000:Dispose objects before losing scope", Justification = "Because disposing the object would write to the response stream, you don't want to prematurely dispose of this object.")]
        private static MvcAnchor AnchorHelper(this HtmlHelper htmlHelper, string linkAction, IDictionary<string, object> htmlAttributes)
        {
            TagBuilder tagBuilder = new TagBuilder("a");

            tagBuilder.MergeAttribute("href", linkAction);
            tagBuilder.MergeAttributes(htmlAttributes);

            htmlHelper.ViewContext.Writer.Write(tagBuilder.ToString(TagRenderMode.StartTag));
            MvcAnchor theForm = new MvcAnchor(htmlHelper.ViewContext);

            return theForm;
        }
    }

    public class MvcAnchor : IDisposable
    {
        private readonly ViewContext _viewContext;
        private bool _disposed;

        public MvcAnchor(ViewContext viewContext)
        {
            if (viewContext == null)
            {
                throw new ArgumentNullException("viewContext");
            }

            _viewContext = viewContext;
        }

        public void Dispose()
        {
            Dispose(true /* disposing */);
            GC.SuppressFinalize(this);
        }

        protected virtual void Dispose(bool disposing)
        {
            if (!_disposed)
            {
                _disposed = true;
                AnchorExtensions.EndAnchor(_viewContext);
            }
        }

        public void EndForm()
        {
            Dispose(true);
        }
    }
}